﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Coolite.Ext.Web;
using GUESTSMANAGEMENT.Controller;

namespace GUESTSMANAGEMENT.View.Event
{
    public partial class grdRelationship : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public Store GridStore
        {
            get;
            set;
        }

        protected void Page_Init(object sender, EventArgs e)
        {
            try
            {
                this.eRalation.GridStore = this.eventUserStore;
                this.GridStore = this.eventUserStore;
            }
            catch
            {
                Ext.Msg.Alert("Alerta!", "Ocorreu um erro durante a transação! Tente novamente!").Show();
            }
        }

        protected void Refresh(object sender, StoreRefreshDataEventArgs e)
        {
            try
            {
                using (guestsmanagementEntities context = new guestsmanagementEntities())
                {
                    string eve = this.txt_idEvent.Text;
                    gmevent evento = Controller.Event.getByEvent(int.Parse(eve));
                    var selectListGuests = from eventUser in context.gmeventuser.Include("gmuser.nmuser")
                                           from gevent in context.gmevent.Include("gmevent.nmevent")
                                           from user in context.gmuser.Include("gmuser.nmuser")
                                           where evento.CDEVENT == gevent.CDEVENT && gevent.CDEVENT == eventUser.gmevent.CDEVENT && user.CDUSER == eventUser.gmuser.CDUSER
                                           select new { eventUser.CDEVENTUSER, gevent.CDEVENT, user.NMUSER, user.CDUSER, gevent.NMEVENT, eventUser.QTBF, eventUser.QTBM, eventUser.QTFF, eventUser.QTFM };
                    var lista = selectListGuests.ToList();

                    this.eventUserStore.DataSource = lista;
                    this.eventUserStore.DataBind();
                }
            }
            catch
            {
                Ext.Msg.Alert("Alerta!", "Ocorreu um erro durante a transação! Tente novamente!").Show();
            }
        }

        public void EditEventUser(object sender, AjaxEventArgs e)
        {
            try
            {
                string id = e.ExtraParams["id"];
                string idUser = e.ExtraParams["idUser"];
                string idEvent = e.ExtraParams["idEvent"];
                this.eRalation.SetEventUser(Controller.EventUser.RetornarEventUser(int.Parse(id)), Controller.User.RetornarUser(int.Parse(idUser)), Controller.Event.RetornarEvent(int.Parse(idEvent)));
                this.eRalation.ShowEventUser();
            }
            catch
            {
                Ext.Msg.Alert("Alerta!", "Ocorreu um erro durante a transação! Tente novamente!").Show();
            }
        }

        public void ShowGridRelationship()
        {
            this.winGrdRelationship.Show();
        }

        public void SetGridRelationship(gmevent evento)
        {
            try
            {
                using (guestsmanagementEntities context = new guestsmanagementEntities())
                {
                    var selectListGuests = from eventUser in context.gmeventuser.Include("gmuser.nmuser")
                                           from gevent in context.gmevent.Include("gmevent.nmevent")
                                           from user in context.gmuser.Include("gmuser.nmuser")
                                           where evento.CDEVENT == gevent.CDEVENT && gevent.CDEVENT == eventUser.gmevent.CDEVENT && user.CDUSER == eventUser.gmuser.CDUSER
                                           select new { eventUser.CDEVENTUSER, gevent.CDEVENT, user.NMUSER, user.CDUSER, gevent.NMEVENT, eventUser.QTBF, eventUser.QTBM, eventUser.QTFF, eventUser.QTFM };
                    var lista = selectListGuests.ToList();

                    this.eventUserStore.DataSource = lista;
                    this.eventUserStore.DataBind();

                    this.txt_idEvent.Text = evento.CDEVENT.ToString();
                }
            }
            catch
            {
                Ext.Msg.Alert("Alerta!", "Ocorreu um erro durante a transação! Tente novamente!").Show();
            }

        }

        protected void ConfirmDeleteEventUser(object sender, AjaxEventArgs e)
        {
            try
            {
                string idUser = e.ExtraParams["idUser"];
                string idEvent = e.ExtraParams["idEvent"];
                gmevent evento = Controller.Event.RetornarEvent(int.Parse(idEvent));
                gmuser user = Controller.User.RetornarUser(int.Parse(idUser));
                string selectEventUser = this.txt_idEventUser.Text.Trim();
                gmeventuser eventUser = Controller.EventUser.getByEventUser(int.Parse(selectEventUser));
                Controller.EventUser.deletEventUser(eventUser);
                this.GridStore.AddScript(string.Format("{0}.reload();", this.GridStore.ClientID));
                Ext.Msg.Alert("Alerta!", "O promoter <b>" + user.NMUSER + "</b> foi removido da festa <b>" + evento.NMEVENT + "</b>!").Show();
            }
            catch
            {
                Ext.Msg.Alert("Alerta!", "Ocorreu um erro durante a transação! Tente novamente!").Show();
            }

        }
    }
}